<?php
namespace app\admin\controller;
class Img extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!='')$map[] = ['and',"title|uuid","like","%{$d['keyword']}%"];
            if($d['min_time']!='') $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $rs = $this->db->query("select * from img a $w[0] order by sort desc,id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
                'uuid'=>'require|unique:img,uuid',
            ];
            $msg = [];
            check($rule,$msg,$d);

            $info = $this->db->query('select * from img where uuid = ?',[$d['uuid']],1);
            if($info){
                err('图片标识已存在');
            }
            $d['create_time'] = time();
            unset($d['file']);;
            $id = $this->db->insert('img',$d);
            m('Attachment','xcom')->imgSure('img',$id,$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        return $this->fetch();
    }

    public function edit($value='')
    {
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'title'=>'require',
                'uuid'=>"require|unique:img,uuid,{$d['id']}",
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            unset($d['file']);
            $info = $this->db->query('select * from img where uuid = ? and id <> ?',[$d['uuid'],$d['id']],1);
            if($info){
                err('图片标识已存在');
            }
            $id = $this->db->update('img',$d);
            m('Attachment','xcom')->imgSure('img',$d['id'],$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from img where id=?",[$id],1);
        return $this->fetch(['info'=>$info]);
    }

    public function del(){
      if(is_post()){
        $id = intval($this->post["id"]);
        $rs = $this->db->delete("img",$id);
        m('Attachment','xcom')->imgDel('img',$id,true);
        if($rs){
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }
    }

    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('img',$id);
                m('Attachment','xcom')->imgDel('img',$id,true);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }

}
